package com.hframe.type;

import com.hframe.data.Base;

public class T<T> implements Base {
    protected T object = null;
    protected TFilter filter = null;
    protected Class<?> className = null;

    public T() {
    }

    public T(T object) {
        this.object = object;
    }

    public T(Class<T> className) {
        this.className = className;
    }

    public T(T object, Class<T> className) {
        this.object = object;
        this.className = className;
    }

    public Class<?> getClassName() {
        return className;
    }

    public T get() {
        return object;
    }

    public void set(T object) {
        this.object = object;
    }

    public Object get(Class<? extends TFilter> filterkey) throws Exception {
        TFilter filter = TFilter.find(this, filterkey);
        if (null != filter)
            return filter.get();

        return object;
    }

    public void set(Class<? extends TFilter> filterkey, T object) throws Exception {
        filter = TFilter.find(this, filterkey);
        if (null != filter)
            filter.set(object);
        else
            this.object = object;
    }

    public <E extends TFilter> E filter(Class<E> filterkey) {
        TFilter filter = TFilter.getNext(this, filterkey);
        filter.parent = this;
        return (E) filter;
    }

    public <E extends TFilter> E getfilter(Class<E> filterkey) {
        return (E) TFilter.find(this, filterkey);
    }

    public boolean isEmpty() {
        return null == object;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        com.hframe.type.T<T> ret = (com.hframe.type.T<T>) super.clone();
        ret.object = null;
        return ret;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;

        if (null != object) {
            if (null == obj)
                return false;

            return obj instanceof com.hframe.type.T<?> ? object.equals(((com.hframe.type.T<?>) obj).object) : object.equals(obj);
        } else {
            return null == obj ? true : obj.equals(object);
        }
    }

    @Override
    public String toString() {
        return null != object ? object.toString() : null;
    }

    public String out() {
        return null != object ? object.toString() : "";
    }

    public static class Number<T> extends com.hframe.type.T<T> {
        public Number() {
        }

        public Number(T object) {
            super(object);
        }
    }
}
